package com.zhz.project.main.socolar.issue;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.tags.Bullet;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.util.getWebContent;

public class SocolarIssueParser {

    String                content = null;
    private static String bStart  = "<b>";
    private static String bEnd    = "</b>";

    public List<SocolarYearRecord> parse() {
        NodeList ul = null;

        Stack<SocolarYearRecord> socolarYearRecordStack = new Stack<SocolarYearRecord>();

        try {
            Parser parser = new Parser(content);

            NodeFilter[] nodeFilter = new NodeFilter[2];

            nodeFilter[0] = new TagNameFilter("ul");

            nodeFilter[1] = new HasAttributeFilter("class", "outside");

            AndFilter andFilter = new AndFilter(nodeFilter);

            ul = parser.extractAllNodesThatMatch(andFilter);

            // 只有一个ul，因此这个循环只有一次
            for (int i = 0; i < ul.size(); i++) {

                /**
                 * 首先解析到的是<b>2010</b>标签， 而后解析到的是 <li class="pr">xxx</li>
                 * 
                 * 因此需要靠栈在这里记住 <li class="pr">xxx</li>所对应的前面的<b>2010</b>
                 * 
                 **/
                List<SocolarIssueRecord> issueList = new ArrayList<SocolarIssueRecord>();
                Stack<String> titleStack = new Stack<String>();

                Node node = ul.elementAt(i);

                NodeList children = node.getChildren();

                boolean bBlock = false;

                for (int k = 0; k < children.size(); k++) {

                    Node child = children.elementAt(k);

                    if (child instanceof TagNode) {
                        TagNode tagNode = (TagNode) child;

                        if (StringUtil.contains(tagNode.toHtml(), bStart)) {
                            // 开始一个<b>
                            bBlock = true;
                            if (issueList.size() > 0) {
                                String year = titleStack.peek();

                                if (StringUtil.isNotBlank(year)) {

                                    // 取出之前解析到的一个年份,构造对象，将解析到的issue信息放入
                                    year = titleStack.pop();

                                    SocolarYearRecord record = new SocolarYearRecord();
                                    record.setYear(year);
                                    record.getSocolarIssueRecordList().addAll(issueList);
                                    socolarYearRecordStack.push(record);
                                    issueList.clear();
                                }
                            }

                        }
                        if (StringUtil.contains(tagNode.toHtml(), bEnd)) {
                            // 结束标记</b>
                            // 在<b>2010</b>中的文本时年份
                            bBlock = false;
                        }

                    }
                    if (child instanceof TextNode) {
                        TextNode textNode = (TextNode) child;

                        if (bBlock) {
                            // 解析到一个年份，存入栈中
                            titleStack.push(StringUtil.trim(textNode.toHtml()));
                        }
                    }

                    if (child instanceof Bullet) {
                        Bullet bullet = (Bullet) child;

                        NodeList bulletchildren = bullet.getChildren();

                        for (int kk = 0; kk < bulletchildren.size(); kk++) {
                            Node bulletchild = bulletchildren.elementAt(kk);

                            if (bulletchild instanceof LinkTag) {

                                // 解析到一个issue的信息
                                LinkTag linkTag = (LinkTag) bulletchild;

                                SocolarIssueRecord socolarIssueRecord = new SocolarIssueRecord();

                                socolarIssueRecord.setIssue(StringUtil.replace(
                                    linkTag.toPlainTextString(), "&nbsp;", " "));
                                socolarIssueRecord.setUrl(linkTag.getLink());
                                issueList.add(socolarIssueRecord);
                            }
                        }
                    }

                    //下一页
                    if (child instanceof LinkTag) {
                        LinkTag linkTag = (LinkTag) child;

                        if (StringUtil.contains(linkTag.getLinkText(), "下一页")) {

                            SocolarIssueParser socolarIssueParser = new SocolarIssueParser();
                            socolarIssueParser.setContent(getWebContent
                                .getContent(SocolarIssueMain.socolar + linkTag.getLink()));
                            List<SocolarYearRecord> tempList = socolarIssueParser.parse();
                            socolarYearRecordStack.addAll(tempList);
                        }
                    }

                }

                if (titleStack.size() > 0) {
                    SocolarYearRecord record = new SocolarYearRecord();
                    record.setYear(titleStack.pop());
                    record.getSocolarIssueRecordList().addAll(issueList);
                    socolarYearRecordStack.push(record);
                }
            }
            return socolarYearRecordStack.subList(0, socolarYearRecordStack.size());
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return null;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}
